import 'package:flutter/material.dart';
import 'package:flutter_snail/pages/reading/follow_list.dart';
import 'package:flutter_snail/pages/reading/punch_list.dart';
import 'package:flutter_snail/pages/reading/recommend_list.dart';
import 'package:flutter_snail/ui/style/snail_color.dart';
import 'package:flutter_snail/ui/style/snail_text_style.dart';
import 'package:flutter_snail/widgets/tool/base_state.dart';
import 'package:flutter_snail/widgets/tool/keep_alive_wrapper.dart';
import 'package:pageview_link_list/pageview_link_list.dart';

/// 领读
class ReadingPage extends StatefulWidget {
  const ReadingPage({Key? key}) : super(key: key);

  @override
  State<ReadingPage> createState() => _ReadingPageState();
}

class _ReadingPageState extends BaseState<ReadingPage>
    with TickerProviderStateMixin {
  final List<String> titleList = ['推荐', '关注', '#川端打卡'];

  late TabController _tabController;

  late PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: titleList.length, vsync: this);
    _pageController = PageController();
  }

  @override
  void dispose() {
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(),
      body: _buildBody(),
    );
  }

  _buildAppBar() {
    return AppBar(
      elevation: 0,
      centerTitle: false,
      title: TabBar(
        tabs: tabs,
        controller: _tabController,
        unselectedLabelColor: SnailColor.greyCD,
        labelColor: SnailColor.black54,
        labelStyle: SnailTextStyle.black54_18m,
        indicatorSize: TabBarIndicatorSize.label,
        isScrollable: true,
        onTap: (index) => _pageController.jumpToPage(index),
      ),
      actions: [_buildWriteBookReview],
    );
  }

  List<Widget> get tabs => titleList.map((e) => Tab(text: e)).toList();

  Widget get _buildWriteBookReview => Center(
        child: InkWell(
          onTap: () {},
          child: const Padding(
            padding: EdgeInsets.all(8.0),
            child: Text('写书评', style: SnailTextStyle.brownAF733E_16),
          ),
        ),
      );

  _buildBody() {
    return PageViewLink(
      pageController: _pageController,
      builder: (_, controllerList) {
        return PageView(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(),
          children: [
            KeepAliveWrapper(child: RecommendList(controller: controllerList[0])),
            const KeepAliveWrapper(child: FollowList()),
            const KeepAliveWrapper(child: PunchList())
          ],
        );
      },
      tabController: _tabController,
    );
  }
}
